package com.wgj.android.http;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.wgj.android.http.model.ProgressModel;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;
/**
 * ========================================
 * 作    者： 佳爱网络有限公司(wwww.jiaailove.com)
 * 版    本： 1.0
 * 创建日期：  2017/12/5
 * 描    述：
 * 修订历吏：
 * =========================================
 */
public class ProgressResponseBody extends ResponseBody {
  public static final int UPDATE = 0x01;
  public static final String TAG = ProgressResponseBody.class.getName();
  private ResponseBody responseBody;
  private OKHttpEngine.ProgressListener mListener;
  private BufferedSource bufferedSource;
  private Handler myHandler;
  public ProgressResponseBody(ResponseBody body, OKHttpEngine.ProgressListener listener) {
    responseBody = body;
    mListener = listener;
    if (myHandler==null){
      myHandler = new MyHandler();
    }
  }
 
  /**
   * 将进度放到主线程中显示
   */
  class MyHandler extends Handler {
 
    public MyHandler() {
      super(Looper.getMainLooper());
    }
 
    @Override
    public void handleMessage(Message msg) {
      switch (msg.what){
        case UPDATE:
          ProgressModel progressModel = (ProgressModel) msg.obj;
          //接口返回
          if (mListener!=null)mListener.onProgress(progressModel.getTotalBytesRead(),progressModel.getContentLength(),progressModel.isComplete());
          break;
 
      }
    }
  }
 
  @Override
  public MediaType contentType() {
    return responseBody.contentType();
  }
 
  @Override
  public long contentLength() {
    return responseBody.contentLength();
  }
 
  @Override
  public BufferedSource source() {
 
    if (bufferedSource==null){
      bufferedSource = Okio.buffer(mySource(responseBody.source()));
    }
    return bufferedSource;
  }
 
  private Source mySource(Source source) {
 
    return new ForwardingSource(source) {
      long totalBytesRead = 0L;
      @Override
      public long read(Buffer sink, long byteCount) throws IOException {
        long bytesRead = super.read(sink, byteCount);
        totalBytesRead +=bytesRead!=-1?bytesRead:0;
        //发送消息到主线程，ProgressModel为自定义实体类
        Message msg = Message.obtain();
        msg.what = UPDATE;
        msg.obj = new ProgressModel(totalBytesRead,contentLength(),totalBytesRead==contentLength());
        myHandler.sendMessage(msg);
 
        return bytesRead;
      }
    };
  }
}